var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "./shopping", "./station", "maishu-wuzhui", "components/common/base-component", "maishu-wuzhui", "./activity", "./account"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const shopping_1 = require("./shopping");
    const station_1 = require("./station");
    const wuzhui = require("maishu-wuzhui");
    const base_component_1 = require("components/common/base-component");
    const maishu_wuzhui_1 = require("maishu-wuzhui");
    const activity_1 = require("./activity");
    const account_1 = require("./account");
    let w = wuzhui;
    let shopping = base_component_1.createService(shopping_1.ShoppingService);
    let station = base_component_1.createService(station_1.StationService);
    let activity = base_component_1.createService(activity_1.ActivityService);
    let account = base_component_1.createService(account_1.AccountService);
    // station.error.add((sender, err) => app.error.fire(app, err, app.currentPage));
    // export let pageData = new wuzhui.DataSource<PageData>({
    //     primaryKeys: ['id'],
    //     select: (args) => {
    //         //===================================================
    //         // 服务端过滤有 BUG，这么写无效
    //         // Type is null or Type == 'page'
    //         args.filter = `(Type is null) or (Type == 'page')`;
    //         //===================================================
    //         return station.pageList(args);
    //     },
    //     insert: (item, args: { isSystem }) => {
    //         item.type = 'page';
    //         return station.savePageData(item);
    //     },
    //     update: (item) => {
    //         return station.savePageData(item)
    //     },
    //     delete: (item) => station.deletePageData(item.id)
    // })
    // export let productTemplate = Object.assign(new w.DataSource<PageData>({
    //     primaryKeys: ['id'],
    //     select: (args) => station.productTemplateList(args),
    //     insert: (item, args: { isSystem }) => station.savePageData(item),
    //     update: (item) => station.savePageData(item),
    //     delete: (item) => station.deletePageData(item.id)
    // }), { count: new chitu.ValueStore() })
    let productDataSource = new wuzhui.DataSource({
        primaryKeys: ['Id'],
        select: (args) => shopping.products(args),
        delete: (item) => shopping.deleteProduct(item.Id),
        insert: (item, args) => {
            args = args || {};
            return shopping.saveProduct({
                product: item, parentId: args.parentId, id: args.id
            });
        },
        update: (item, args) => {
            args = args || {};
            return shopping.saveProduct({
                product: item, parentId: args.parentId, id: args.id
            });
        }
    });
    let categoryDataSource = new maishu_wuzhui_1.DataSource({
        primaryKeys: ["Id"],
        select: (args) => __awaiter(void 0, void 0, void 0, function* () {
            let dataItems = yield shopping.categories();
            return { dataItems, totalRowCount: dataItems.length };
        }),
        insert: (item) => __awaiter(void 0, void 0, void 0, function* () {
            let r = yield shopping.addCategory(item);
            return r;
        }),
        delete: (item) => __awaiter(void 0, void 0, void 0, function* () {
            yield shopping.deleteCategory(item.Id);
        })
    });
    let brandDataSource = new maishu_wuzhui_1.DataSource({
        primaryKeys: ["Id"],
        select: (args) => __awaiter(void 0, void 0, void 0, function* () {
            let r = yield shopping.brands(args);
            return r;
        }),
        insert: (item) => __awaiter(void 0, void 0, void 0, function* () {
            let r = yield shopping.addBrand(item);
            return r;
        }),
        delete: (item) => __awaiter(void 0, void 0, void 0, function* () {
            yield shopping.deleteBrand(item);
        })
    });
    let couponDataSource = new maishu_wuzhui_1.DataSource({
        primaryKeys: ['Id'],
        select: (args) => shopping.coupons(args),
        delete: (dataItem) => shopping.deleteCoupon(dataItem),
        update: (dataItem) => shopping.saveCoupon(dataItem),
        insert: (dataItem) => shopping.saveCoupon(dataItem),
    });
    let activityDataSource = new wuzhui.DataSource({
        primaryKeys: ['Id'],
        select: () => activity.activities(),
        insert: (dataItem) => activity.addActivity(dataItem),
        delete: (dataItem) => activity.deleteActivity(dataItem),
        update: (dataItem) => activity.updateActivity(dataItem)
    });
    let receivementAccount = new w.DataSource({
        primaryKeys: ['Id'],
        select: () => __awaiter(void 0, void 0, void 0, function* () {
            let dataItems = yield account.receivementAccountList();
            return { dataItems, totalRowCount: dataItems.length };
        }),
        insert: (item) => account.saveReceivementAccount(item),
        update: (item) => account.saveReceivementAccount(item)
    });
    exports.dataSources = {
        product: productDataSource,
        category: categoryDataSource,
        brand: brandDataSource,
        activity: activityDataSource,
        coupon: couponDataSource,
        account: receivementAccount,
    };
});
